// 公司设置
<template>
  <div class="setting-container">
    <el-card>
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="one">
          <el-row style="height: 60px">
            <el-button type="primary" icon="el-icon-plus" @click="showDialog = true">新增角色</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table :data="list" style="width: 100%" border stripe>
            <el-table-column label="序号" width="100" type="index" align="center" />
            <el-table-column prop="name" label="名称" width="200" align="center" />
            <el-table-column prop="description" label="描述" align="center" />
            <el-table-column label="操作" width="240" align="center">
              <template slot-scope="{ row }">
                <el-button type="success" size="small" @click="assignPerm(row.id)">分配权限</el-button>
                <el-button type="primary" size="small" @click="editRole(row.id)">编辑</el-button>
                <el-button type="danger" size="small" @click="deleteRole(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-row type="flex" justify="center" style="height: 60px" align="middle">
            <el-pagination
              :current-page="page.page"
              :page-sizes="[2, 5, 10, 15]"
              :page-size="page.pagesize"
              layout="total,sizes,prev, pager, next"
              :total="page.total"
              @current-change="changePage"
              @size-change="changePageSize"
            />
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="two">
          <el-alert
            title="对公司名称、公司地址、公司电话、公司邮箱的更新,请谨慎修改!"
            :closable="false"
            type="info"
            show-icon
          />
          <el-form label-width="120px" style="margin-top: 50px">
            <el-form-item label="企业名称">
              <el-input v-model="formData.name" disabled style="width: 400px" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="formData.companyAddress" disabled style="width: 400px" />
            </el-form-item>
            <el-form-item label="电话">
              <el-input v-model="formData.companyPhone" disabled style="width: 400px" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="formData.mailbox" disabled style="width: 400px" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="formData.remarks" disabled style="width: 400px" type="textarea" rows="3" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 编辑弹出层 -->
    <!-- 点击确定 也会触发close事件 因为点击确认 就会关闭dialog 因此触发close事件 -->
    <el-dialog title="编辑部门" :visible="showDialog" @close="btnCancel">
      <el-form ref="roleForm" label-width="120px" :model="roleForm" :rules="rules">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <el-row type="flex" justify="center" align="middle">
        <el-col :span="8">
          <el-button type="" size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限弹出层 -->
    <el-dialog title="分配权限" :visible.sync="showPerDialog" @close="brnPermCancel">
      <el-tree
        ref="permTree"
        :data="permData"
        :default-checked-keys="selectCheck"
        show-checkbox
        default-expand-all
        node-key="id"
        highlight-current
        check-strictly
        :props="defaultProps"
      />
      <el-row slot="footer" type="flex" justify="center" align="middle"">
        <el-col :span="6">
          <el-button size="small" type="primary" @click="btnPermOk">确定</el-button>
          <el-button size="small" @click="brnPermCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { getRoleList, getCompanyInfo, deleteRole, getRole, updateRole, addRole, assignPerm } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeDate } from '@/utils'
import { mapGetters } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'one',
      list: [],
      page: {
        page: 1,
        pagesize: 5,
        total: 0
      },
      formData: {},
      showDialog: false,
      showPerDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '角色名称不能为空' }]
      },
      permData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      roleId: '',
      selectCheck: []
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 获取角色列表
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    // 页码改变 重新获取角色列表
    changePage(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    changePageSize(newPageSize) {
      this.page.pagesize = newPageSize
      this.page.page = 1
      this.getRoleList()
    },
    // 通过id获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
    },
    // 删除角色
    async deleteRole(id) {
      try {
        await this.$confirm('确认删除该角色么？')
        if (this.list.length === 1) {
          this.page.page > 1 ? this.page.page-- : this.page.page
        }
        await deleteRole(id)
        this.getRoleList()
        this.$message.success('成功删除角色')
      } catch (error) {
        this.$message('已经取消删除')
      }
    },
    // 编辑角色
    async editRole(id) {
      this.roleForm = await getRole(id)
      this.showDialog = true
    },
    // 点击取消编辑角色
    btnCancel() {
      this.showDialog = false
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除效验规则提示  它也会重置 有效验规则 的表单
      this.$refs.roleForm.resetFields()
    },
    // 新增、编辑角色
    async btnOk() {
      try {
        await this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          // 编辑
          await updateRole(this.roleForm)
        } else {
          // 新增
          await addRole({
            name: this.roleForm.name,
            region: this.roleForm.description
          })
        }
        this.getRoleList()
        this.showDialog = false
        this.$message.success('操作成功')
      } catch (error) {
        console.log('检验失败')
      }
    },
    // 分配权限
    async assignPerm(id) {
      this.roleId = id
      const result = await getPermissionList()
      this.permData = tranListToTreeDate(result, '0')
      const { permIds } = await getRole(id)
      this.selectCheck = permIds
      this.showPerDialog = true
    },
    // 点击确定
    async btnPermOk() {
      const result = this.$refs.permTree.getCheckedKeys()
      await assignPerm({ id: this.roleId, permIds: result })
      this.$message.success('分配权限成功！')
      this.showPerDialog = false
    },
    // 点击取消
    brnPermCancel() {
      this.selectCheck = []
      this.showPerDialog = false
    }
  }
}
</script>

<style scoped lang="scss">
.setting-container {
  padding: 15px;
  ::v-deep .el-textarea__inner {
    resize: none;
  }
  ::v-deep .el-dialog__header {
    background-color: rgb(29, 63, 255);
    .el-dialog__title,
    .el-dialog__close {
      color: #fff;
    }
  }
}
</style>
